<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <style>
        header a {
            text-decoration: none;
            color: #666;
        }

    </style>


</head>
<body>
<div id="app">
    <el-container>
        <el-header height="150px">
            <div style="width: 1200px;margin: 0 auto">
                <img src="imgs/logo.png"
                     style="width: 300px;vertical-align: middle" alt="">
                <a href="/">首页</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">热点资讯</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">商家入驻</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">社会招聘</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">校园招聘</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">帮助</a>
            </div>
            <div style="background-color: #82c8ec">
                <el-menu text-color="#fff" background-color="#82c8ec"
                         mode="horizontal"
                         @select="handleSelect"
                         style="width: 1200px;margin: 0 auto"
                         active-text-color="#fff">
                    <el-menu-item v-for="c in categoryArr"
                                  :index="c.id">{{c.name}}
                    </el-menu-item>

                    <div style="float: right;position: relative;top: 10px">
                        <el-input v-model="wd" placeholder="请输入搜索的内容">
                            <el-button @click="search()" slot="append" icon="el-icon-search"></el-button>
                        </el-input>
                    </div>
                </el-menu>
            </div>

        </el-header>
        <el-main style="width: 1200px;margin: 0 auto">

            <!--商品列表开始-->
            <el-row gutter="20">
                <el-col style="margin: 10px 0" span="6" v-for="p in productArr">
                    <el-card>
                        <a :href="'/detail.html?id='+p.id">
                            <img :src="p.url" width="233" height="233" alt="">
                        </a>
                        <div>
                            <p style="font-size: 15px;height: 40px">
                                <a style="text-decoration: none;color: #3f3f3f" :href="'/detail.html?id='+p.id">
                                    {{p.title}}
                                </a>
                            </p>
                            <p>￥{{p.price}} <s>{{p.oldPrice}}</s>
                                <span style="float: right">销量:{{p.saleCount}}件</span>
                            </p>
                        </div>
                    </el-card>
                </el-col>
            </el-row>
            <!--商品列表结束-->
        </el-main>
        <el-footer>
            <div style="background-image: url('imgs/wave.png');height: 95px"></div>
            <div style="background-color: #3f3f3f;color: #b1b1b1;
            text-align: center;padding: 30px 0">
                <p>Copyright © 北京达内金桥科技有限公司版权所有 京ICP备12003709号-3 京公网安备 11010802029572号</p>
                <p>达内在线WWW.TMOOC.CN 专注于IT职业技能培训</p>
                <p>涵盖20余门课程体系，致力于打造权威的IT职业教育学习平台</p>
            </div>
        </el-footer>
    </el-container>

</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                categoryArr: [],
                productArr: [],
                wd:""
            }
        },
        methods: {
            search(){
                axios.get("/product/selectByWd?wd="+v.wd).then(function (response) {
                    v.productArr = response.data;
                })
            },
            handleSelect(key, keyPath) {
                //请求点击的这个分类下的所有商品
                axios.get("/product/selectByCid?id=" + key)
                    .then(function (response) {
                        v.productArr = response.data;
                    })
            }
        },
        created: function () {
            // x.indexOf(y) 获取y在x中出现的位置(从0开始) 如果x里面没有y则返回-1
            //判断地址栏中是否包含wd,如果包含代表要显示搜索结果 !=-1代表包含
            //包含则通过搜索的关键字进行搜索请求,不包含代表点击的是分类 查询分类下的商品
            let url = location.search.indexOf("wd") != -1 ?
                "/product/selectByWd" : "/product/selectByCid";
            //通过分类id查询这个分类下的所有商品
            axios.get(url + location.search).then(function (response) {
                v.productArr = response.data;
            })
            //请求所有分类数据
            axios.get("/category/select").then(function (response) {
                v.categoryArr = response.data;
            })
        }
    })
</script>
</html>